<?php

declare(strict_types=1);

namespace App\Controller\Admin;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use App\Middleware\AdminMiddleware;
use App\Model\User;

/**
 * @AutoController(prefix="/admin/member")
 * @Middlewares({
 *     @Middleware(AdminMiddleware::class)
 * })
 */
class Member extends AbstractController
{
    public function list() {
        $data = $this->request->all();
        $query = User::query();

        if (!empty($data['nickname'])) {
            $query = $query->where('nickname', 'like', '%'.$data['nickname'].'%');
        }
        if (!empty($data['phone'])) {
            $query = $query->where('phone', 'like', '%'.$data['phone'].'%');
        }

        $query = $query->orderBy('id', 'desc');
        if (isset($data['page_size']) && $data['page_size'] <= 0) {
            $list = $query->get();
        }else{
            $list = $query->paginate($data['page_size'] ?? 20);
        }
        return $this->success($list);
    }
}
